Start typing to search every page on this site.

Use ↑ and ↓ to move through results, then Enter to open one.

    Skip to main content

    16. The Hamming cube and Hamming distance#

    In Lecture Note 1 we described a transmission by an encoding, a channel, and a decoder, and we found that reliable transmission of long messages needs codes that correct many errors in each block. In this note we study what a decoder should do and which codes allow it to correct a given number of errors. We begin with an example.

    Consider the code with four code words of length , used on the binary symmetric channel with . The receiver gets . This is not a code word, so at least one error occurred. Which code word was most likely sent?

    The word 01100 differs from 00000 in positions, from 11100 in position, from 00111 in positions, and from 11011 in positions. By Proposition 15.10 of Lecture Note 1, if a code word that differs from in positions is sent, then is received with probability Since , the number is less than , so this probability is largest when is smallest. The code word most likely to have been sent is therefore 11100, which differs from in a single position.

    The number of positions in which two words differ is the Hamming distance, and the example suggests that a good decoder chooses the code word closest to the received word. We prove this in general, and we then use the distance to determine how many errors a code can detect or correct. Counting the words close to each code word leads to an upper bound on the number of code words, the Hamming bound, and we end with the Hamming code, whose size equals this bound.

    After studying these notes, you will be able to do the following.

    • Compute Hamming distances and use the triangle inequality for them.

    • Explain why, on with , the most likely transmitted code word is a nearest code word.

    • Find the minimum distance of a code and use it to determine how many errors the code detects or corrects.

    • Use the distance criterion for a decoder that both corrects some errors and reports others.

    • List the possible sent code words under a bound on the number of errors, and determine which messages and error positions can be recovered.

    • Count the words in a Hamming ball, prove the Hamming bound, and recognize perfect codes.

    • Encode and decode with the Hamming code of length .

    Throughout, is the set of binary words of length , is the Hamming weight of (the number of s in ), and is the binary symmetric channel on words of length with flip probability , as in Lecture Note 1.

    16.1 Hamming distance and the Hamming cube#

    Definition 16.1 (Hamming distance)#

    The Hamming distance between two words is the number of positions with .

    The sum has a exactly in the positions where and differ, so For example, . If a code word is sent and the channel produces the error pattern , the received word is , and . Thus the distance between the transmitted word and the received word is the number of errors.

    Proposition 16.2 (Properties of Hamming distance)#

    For all :

    1. , with equality exactly when ;

    2. ;

    3. .

    Proof#

    Properties (1) and (2) follow directly from the definition. For (3), consider a position with . Since is or , it cannot be equal to both and , so or . Hence the set of positions where and differ is contained in the union of the set of positions where and differ and the set of positions where and differ. The size of a union of two sets is at most the sum of their sizes, and this gives (3).

    Property (3) is the triangle inequality. Ordinary distance in the plane has the same three properties, and a function with these properties is called a metric.

    The Hamming distance is also a distance in a graph. The Hamming cube is the graph whose vertices are the words of , with an edge between two words that differ in exactly one position. For , if each word is placed at the point of space, is the ordinary cube shown in Figure 16.2.

    Proposition 16.3 (Distance in the Hamming cube)#

    For all , the length of a shortest path from to in is .

    Proof#

    Let . Starting from and flipping, one at a time, the positions where and differ gives a path of length from to . Conversely, each edge of a path changes exactly one position, and each of the positions where and differ must be changed at least once, so every path from to has at least edges.

    16.2 Maximum likelihood decoding#

    When the receiver gets a word , it has to choose a code word. In this section we regard a decoder as a map that returns a code word; since the encoding is injective, the code word determines the message.

    Definition 16.4 (Maximum likelihood decoder)#

    A decoder is a maximum likelihood decoder if, for every received word , the probability that is received when is sent is at least as large as the probability that is received when is sent, for every code word . In other words, is a code word that is most likely to produce . If several code words give the same largest probability, the decoder may choose any of them.

    A maximum likelihood decoder is correct as often as possible when every code word is sent with the same probability . Indeed, the decoder is correct when the received word is and the code word sent was , so its probability of being correct is The value can be chosen separately for each received word , and a maximum likelihood decoder chooses it so that each term of the sum is as large as possible.

    Theorem 16.5 (Maximum likelihood decoding is nearest code word decoding)#

    On with , a decoder is a maximum likelihood decoder if and only if, for every , the code word has the smallest Hamming distance to among all code words.

    Proof#

    By Proposition 15.10 of Lecture Note 1, the probability that is received when is sent is Since , this probability decreases as increases. So for a fixed , a code word makes the probability that is received when is sent largest exactly when it makes smallest.

    A decoder that always returns a code word at the smallest distance from the received word is called a nearest code word decoder. For triple repetition, the code word nearest to is the one that agrees with in at least two of the three positions, so majority vote is a nearest code word decoder and hence a maximum likelihood decoder.

    16.3 Minimum distance#

    Whether a nearest code word decoder recovers the transmitted word depends on how far apart the code words are.

    Definition 16.6 (Minimum distance)#

    Let be a code with at least two code words. The minimum distance is the smallest Hamming distance between two different code words of . A code of block length with code words and minimum distance is called an code.

    Definition 16.7 (Hamming ball)#

    For and an integer , the Hamming ball of radius around is

    The ball is the set of words that can be received when is sent and at most errors occur.

    Definition 16.8 (Detecting and correcting errors)#

    Let be a code, and let be integers.

    • detects up to errors if changing at least and at most bits of a code word never produces another code word.

    • corrects up to errors if there is a decoder such that whenever and .

    If detects up to errors, the receiver can reject every received word that is not a code word, and every transmission with between and errors is rejected.

    Theorem 16.9 (Detection and correction)#

    Let be a code with minimum distance .

    1. detects up to errors if and only if .

    2. For every integer , the following statements are equivalent:

      1. corrects up to errors;

      2. the balls , for , are pairwise disjoint;

      3. .

      When these statements hold, every nearest code word decoder corrects up to errors.

    Proof#

    For (1): changing between and bits of a code word produces exactly the words with . One of these is a code word exactly when two different code words are at distance at most , that is, when .

    For (2) we prove that (c) implies (b), (b) implies (a), and (a) implies (c).

    Assume (c). If a word lay in both and for different code words and , the triangle inequality would give , which contradicts the definition of . So the balls are disjoint.

    Assume (b). Then each word lies in at most one of the balls. Let be the center of that ball, and let be an arbitrary code word if lies in none of the balls. If is sent and at most errors occur, the received word lies in , so returns .

    Assume (a), and suppose, to reach a contradiction, that . Take code words with . Starting from , flip of the positions where and differ, and call the result . Then and . A decoder that corrects up to errors would have to return and also , which is impossible.

    Finally, assume , let be sent, and let be received with . For every other code word , the triangle inequality gives . So is the only code word nearest to , and every nearest code word decoder returns .

    Figure 16.1 illustrates part (2) along a shortest path between two code words, for . When , the word in the middle of the path lies in both balls, and a decoder cannot know whether or was sent. When , the two balls are separated.

    Two rows of dots joined by a line. The top row has five dots; the first is labeled x, the middle y, the last x prime. The first three dots are shaded blue, the last three red, and the middle dot, in both groups, is purple. The bottom row has six dots from x to x prime; the first three are blue, the last three red, and no dot is in both groups.
    Figure 16.1: Shortest paths in the Hamming cube between two code words and , with the words within distance of shaded blue and those within distance of shaded red. Top: , and the middle word (purple) is within distance of both. Bottom: , and no word on the path is within distance of both.#

    Figure 16.2 shows the whole Hamming cube for triple repetition, which has and corrects one error: the two balls of radius are disjoint and together contain all eight words.

    A cube whose eight corners are labeled with the binary words of length 3, with edges joining words that differ in one bit. The corners 000, 100, 010, 001 are shaded blue, and the corners 111, 011, 101, 110 are shaded red. The corners 000 and 111 are outlined more heavily.
    Figure 16.2: The Hamming cube of length . The code words 000 and 111 have heavy outlines. The ball of radius around 000 is shaded blue and the ball of radius around 111 is shaded red; a nearest code word decoder sends each word to the code word in its ball.#

    By Theorem 16.9, a code with minimum distance detects up to errors and corrects up to errors.

    Example 16.10 (Parameters of our codes)#
    codedetects up tocorrects up to
    triple repetition
    parity check
    the code of the opening example

    In the parity-check code, every code word has an even number of s. Changing one bit makes the number of s odd, so two code words never differ in exactly one position; changing two bits of a code word gives another code word. Hence . For the code of the opening example, the distances between the six pairs of code words are so .

    A code with can be used to detect up to errors, or to correct error, but one decoder cannot do both. Suppose the receiver for triple repetition corrects single errors, 000 is sent, and two errors produce 011. The nearest code word to 011 is 111, so the receiver returns the wrong code word and does not notice that anything went wrong.

    16.3.1 Correction and detection with one decoder#

    Suppose the receiver must correct every pattern of up to errors and report failure whenever more than but at most errors occur. The received words that require correction must be separated from the words that could come from a different code word with up to errors.

    Theorem 16.11 (Correction and detection with one decoder)#

    Let be a binary code with at least two code words and minimum distance , and let be integers. There is a decoder such that, for every sent code word and every received word , if and only if

    Proof#

    First suppose . Since , we also have , so the balls of radius around the code words are disjoint. Define to be the code word within distance of if there is one, and to be failure otherwise.

    If is sent and at most errors occur, then lies in the ball around , so . Now suppose . The word lies outside the ball around . If it lay within distance of another code word , the triangle inequality would give which is impossible. Thus lies in none of the balls, and is failure.

    Conversely, suppose such a decoder exists but . Choose different code words at distance . Starting from , flip of the positions where and differ, obtaining . Then The last inequality holds because if , and otherwise. If was sent, the decoder must return . If was sent, it must return when , or failure when . Neither output equals , so the requirements contradict each other. Hence .

    Example 16.12 (Correcting one error and reporting two errors)#

    Consider fourfold repetition, with and . The two code words have distance . A decoder can therefore correct one error and report every two-error pattern, since . It returns a code word when the received word is within distance of it, and reports failure otherwise.

    For example, 0001 is at distance from 0000 and distance from 1111, so the decoder returns 0000. The word 0011 is at distance from both code words, so it reports failure.

    Fourfold repetition detects up to errors when detection is used on its own, but a decoder that corrects one error cannot also report every three-error pattern: that would require . Indeed, 0001 can result from one error in 0000 or three errors in 1111. Correcting the first transmission forces the decoder to return the wrong code word on the second.

    16.4 Recovery with a bounded number of errors#

    For fourfold repetition, receiving 0001 determines the sent code word if at most one error occurred: it must be 0000. If all we know is that at most three errors occurred, 1111 is also possible. The distances have not changed; the assumption about the number of errors has changed.

    Let be a code, let be the received word, and suppose that no more than errors occurred, where . The set of possible sent code words is Every code word outside this set would require more than flipped bits, so it cannot have been sent under the assumption. Every code word inside this set could have been sent: flipping exactly the positions where it differs from produces the received word using at most errors.

    Consequently, the message can be recovered with certainty exactly when contains one code word. If several code words remain, the distance bound alone does not determine the message, even if one of those code words is strictly closer to than all the others. If is empty, the received word is inconsistent with the assumed bound on the number of errors.

    Each possible sent code word also gives a possible error pattern. For , let These are exactly the positions that flipped if was sent. The error pattern is , and the number of errors is . Even when the message is not determined, the number of errors may be determined: this happens exactly when all the possible sent code words are at the same distance from .

    For a code given by a short list of code words, we can carry out the recovery as follows.

    1. Compute the distance from to each code word.

    2. Keep every code word whose distance is at most .

    3. For each remaining code word, record its message, its distance from , and the positions where it differs from . Compare the possibilities to determine which information is certain.

    Example 16.13 (What the error bound determines)#

    Again use fourfold repetition, with messages encoded as 0000, 1111. The following table records the possible sent code words in three situations.

    Received word Bound
    0001
    0001
    0011

    For 0001 with , the only possible sent code word is 0000. Exactly one error occurred, in position , and the sent message was .

    For the same received word with , there are two possibilities. If 0000 was sent, one error occurred in position . If 1111 was sent, three errors occurred in positions . We cannot determine the number of errors, their positions, or the sent message from the stated bound. Choosing the nearer code word would be a decoding decision, rather than a certain recovery of the message.

    For 0011 with , both possible sent code words are at distance . Thus exactly two errors occurred. If 0000 was sent, positions flipped; if 1111 was sent, positions flipped. The error count is determined, but the error positions and the sent message are not.

    The distance criterion gives a guarantee for every transmission with up to a stated number of errors. The set determines what can be recovered for the particular received word . Even beyond the guaranteed correction radius, a particular received word may still have only one possible sent code word.

    16.5 The Hamming bound#

    By Theorem 16.9, a code that corrects up to errors has pairwise disjoint balls of radius around its code words, and all of these balls lie in . If we know how many words a ball contains, we obtain an upper bound on the number of code words.

    Proposition 16.14 (Size of a Hamming ball)#

    For every and every integer with ,

    Proof#

    A word at distance exactly from is determined by the set of positions in which it differs from , and there are such sets. The ball is the disjoint union of the sets of words at distance exactly from , for , so by the sum rule its size is .

    For example, : the ball of radius consists of the center and the words obtained from it by flipping one bit. For , .

    Theorem 16.15 (The Hamming bound)#

    If a code corrects up to errors, then

    Proof#

    By Theorem 16.9, the balls with are pairwise disjoint, and by Proposition 16.14 each contains words. Their union is contained in , so .

    The Hamming bound is also called the sphere-packing bound, because it compares the total size of disjoint balls with the size of the space that contains them.

    Definition 16.16 (Perfect code)#

    A code that corrects up to errors is perfect if .

    In a perfect code, the balls of radius around the code words contain every word of , so every word is within distance of exactly one code word. Triple repetition is perfect, since ; this is the picture in Figure 16.2.

    16.5.1 Single-error-correcting codes#

    Consider a code that sends message bits in blocks of bits and corrects one error. It has code words, and , so the Hamming bound states that For each number of message bits, substitute the number of code words into the volume bound and find the smallest integer that satisfies it. The last column tests perfection at this smallest allowed block length, by checking whether the bound is an equality.

    Block lengths and perfection for single-error correction

    Message bits
    ()
    Volume bound
    Smallest allowed
    block length ()
    Triple repetition
    block length ()
    Perfect?
    Yes:
    No:
    No:
    Yes:

    The Hamming bound shows that no shorter code exists, but it does not show that a code of the allowed length exists; such a code has to be constructed. For , the code of the opening example is a code; it is obtained from the encoding . For , we construct a code of length in the next section. In both cases the code is shorter than triple repetition.

    A perfect single-error-correcting code requires to divide . The only divisors of are powers of , so for some , and the possible lengths are . Perfect codes that correct more than one error are rare. Repetition codes of odd length , consisting of the all-zero and the all-one word, are perfect and correct errors (Problem 16.19). A theorem proved in the early 1970s states that every other perfect binary code that corrects at least one error has the same length and the same number of code words either as a single-error-correcting code of length or as the Golay code, which has length and code words and corrects errors.

    16.6 The Hamming code of length seven#

    We construct a code with code words of length that corrects one error. A code word consists of four data bits and three parity-check bits . Each parity bit is chosen so that a certain set of positions, called a check, contains an even number of s.

    We choose the checks so that the receiver can locate a single error. When one bit flips, exactly the checks that contain that position fail. The set of checks containing a position is called its signature. If the seven positions have seven different nonempty signatures, then the set of failed checks identifies the flipped position, and an empty set of failed checks means that no single error occurred. Three checks have exactly nonempty subsets, so we can assign one to each position:

    bit
    signature

    Each parity bit lies in only one check, so it can be used to make that check even.

    Three overlapping circles labeled check 1, check 2, check 3. The seven regions contain the bits: p1 only in check 1, p2 only in check 2, p3 only in check 3, d1 in checks 1 and 2, d2 in checks 1 and 3, d3 in checks 2 and 3, and d4 in the center, in all three checks.
    Figure 16.3: The three parity checks of the code of length . Each circle is a check, and each bit lies in the circles of the checks that contain it. Each circle must contain an even number of s.#

    Figure 16.3 draws the checks as three circles. Check contains , check contains , and check contains . A check contains an even number of s exactly when the sum of its bits in is , so the parity bits are Writing each code word in the order , the code words are

    0000000 1110001 0110010 1000011 1010100 0100101 1100110 0010111
    1101000 0011001 1011010 0101011 0111100 1001101 0001110 1111111

    This code is the Hamming code of length .

    Proposition 16.17 (The Hamming code is perfect)#

    The Hamming code of length is a perfect code.

    Proof#

    Different data bits give different code words, so there are code words. Let be a code word, and let be obtained from by flipping the positions in a nonempty set . All checks pass on , and flipping a position changes the parity of exactly the checks in its signature. Hence check fails on exactly when an odd number of positions in have in their signature.

    If has one element, its signature is nonempty, and every check in it fails. If has two elements, their signatures are different, so some check belongs to exactly one of the two signatures, and that check fails. In both cases is not a code word. Thus two different code words differ in at least three positions, and . The code word 1000011 is at distance from 0000000, so , and by Theorem 16.9 the code corrects one error. Finally, , so the code is perfect.

    The signatures also give a decoder that requires no comparison with the code words. This decoder is called syndrome decoding: the receiver computes the set of checks that fail on the received word. If is empty, it returns the received word. Otherwise, it flips the position whose signature is . By the argument above, this decoder returns the transmitted code word whenever at most one error occurred.

    Example 16.18 (Syndrome decoding)#

    The message has parity bits so the transmitted code word is 0101011. Suppose that the sixth bit, , flips, and the receiver gets 0101001. The receiver evaluates the checks:

    • check : , so it passes;

    • check : , so it fails;

    • check : , so it fails.

    The failed checks are , which is the signature of . The receiver flips the sixth bit and recovers 0101011.

    The Hamming code corrects only one error in each block of bits. By Lecture Note 1, this is adequate when is much smaller than , but long messages require long blocks in which about errors are corrected. For such codes, the Hamming bound with close to gives an upper limit on the rate, and finding codes whose rate is close to this limit is one of the main problems of coding theory.

    16.7 Exercises#

    Problem 16.19#

    Let be the repetition code of length .

    1. Show that corrects up to errors.

    2. Using , show that , so that is perfect.

    Problem 16.20#

    Let be message bits, and set , , .

    1. List the code words , and show that the minimum distance is .

    2. Use the Hamming bound to show that no code of length that corrects one error has code words.

    Problem 16.21#

    Let be the Hamming code of length .

    1. Prove that the sum of two code words is a code word.

    2. Show that has one word of weight , seven words of weight , seven words of weight , and one word of weight .

    3. A code that contains the sum of any two of its code words is called linear. Prove that the minimum distance of a linear code is the smallest weight of a nonzero code word. (Use .)

    Problem 16.22#

    Extend each code word of the Hamming code of length by an eighth bit .

    1. Prove that every extended code word has even weight.

    2. Prove that the extended code is an code.

    3. Using Theorem 16.11, determine how many errors the extended code can correct while detecting errors.

    Problem 16.23#

    A perfect code that corrects errors requires to be a power of .

    1. Show that this is equivalent to for some integer .

    2. Find all with that satisfy this equation, and decide for each whether a perfect code of that length correcting errors exists, using the list of perfect codes in Section 16.5.1.

    Problem 16.24#

    The greedy algorithm lists the words of in the order 00000, 00001, 00010, , 11111 and adds a word to the code if its distance from every word already chosen is at least .

    1. List the code words that the algorithm chooses.

    2. Compare the number of code words with the Hamming bound for and .